package com.xianbeiying.app.ckgl.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.downfile.NotificationUpdateActivity;

public class AppChangen {
	public static Dialog dialog;
	public static void showUpdateDialog(final Activity mainActivity,
			final String versionsUrl, final MyApplication app) {
		try {
			LayoutInflater inflater = (LayoutInflater) mainActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
			// 新建自己风格的dialog
			dialog = new Dialog(mainActivity, R.style.Dialog);
			// 绑定布局
			dialog.setContentView(layout);

			Button bt1 = (Button) layout.findViewById(R.id.positiveButton);
			Button bt2 = (Button) layout.findViewById(R.id.negativeButton);
			bt1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (Tools.ExistSDCard()) {
						Intent it = new Intent(mainActivity,
								NotificationUpdateActivity.class);
						mainActivity.startActivity(it);
						app.setDownload(true);
						app.setVersionsUrl(versionsUrl);
						dialog.dismiss();

					} else {
						Toast.makeText(mainActivity, "手机未安装sd卡",
								Toast.LENGTH_SHORT).show();
					}
				}
			});
			bt2.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					dialog.dismiss();
				}
			});
			final ImageView checkBox = (ImageView) layout.findViewById(R.id.checkBox);
			if (SharedPreferencesUtils.getBooleanDate("Check")){
				checkBox.setSelected(false);
				MyApplication.isShowDown=false;
			}
			else{
				checkBox.setSelected(false);
				MyApplication.isShowDown=true;
			}
			checkBox.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					checkBox.setSelected(checkBox.isSelected());
					if(checkBox.isSelected()){
						SharedPreferencesUtils.setBooleanDate("Check",true);
						MyApplication.isShowDown=false;
					}else{
						SharedPreferencesUtils.setBooleanDate("Check",false);
						MyApplication.isShowDown=true;
					}
					// TODO Auto-generated method stub
				}
			});
			dialog.setCancelable(false);
			dialog.setOnKeyListener(new OnKeyListener() {

				@Override
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					// TODO Auto-generated method stub
					if (keyCode == KeyEvent.KEYCODE_BACK
							&& event.getAction() == KeyEvent.ACTION_DOWN) {
						if (dialog == null) {

							return false;
						} else {
							dialog.dismiss();
						}
					}
					return false;
				}
			});
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
